package com.jinlejia.bbs.ui.fun;

import androidx.databinding.DataBindingUtil;
import android.os.Bundle;

import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import android.view.View;

import com.jinlejia.bbs.R;
import com.jinlejia.bbs.adapter.ContributeAdapter;
import com.jinlejia.bbs.bean.base.BBSUserBean;
import com.jinlejia.bbs.databinding.BbsActivityContributeBinding;
import com.jinlejia.bbs.dialog.ContributeResultDialog;
import com.jinlejia.bbs.help.CommonEvent;
import com.jinlejia.bbs.logic.ContributeLogic;

import java.util.ArrayList;

/**
 * 贡献月榜
 */
public class BBSContributeActivity extends AppCompatActivity implements ContributeLogic.LogicCallBack, SwipeRefreshLayout.OnRefreshListener {
    private BbsActivityContributeBinding binding;
    private ContributeLogic logic;
    private ArrayList<BBSUserBean> dataList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.bbs_activity_contribute);
        binding.setEvent(this);
        binding.setCommonEvent(CommonEvent.obtain());
        dataList = new ArrayList<>();
        logic = new ContributeLogic(this,dataList,this);

        initView();
    }

    private ContributeAdapter adapter;
    private void initView() {
        adapter = new ContributeAdapter(this);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        DividerItemDecoration divider = new DividerItemDecoration(this, DividerItemDecoration.VERTICAL);
        divider.setDrawable(getResources().getDrawable(R.drawable.bbs_inset_line_divider));
        binding.recyclerView.addItemDecoration(divider);
        binding.recyclerView.setAdapter(adapter);
        binding.recyclerView.setLayoutManager(layoutManager);
        binding.swipeRefreshLayout.setOnRefreshListener(this);
        binding.swipeRefreshLayout.post(new Runnable() {
            @Override
            public void run() {
                binding.swipeRefreshLayout.setRefreshing(true);
                logic.execute();
            }
        });
    }

    private ContributeResultDialog dialog;
    public void showRule(View view) {
        if (dialog==null){
            dialog = new ContributeResultDialog();
        }
        dialog.show(getSupportFragmentManager(),"Dialog");
    }

    @Override
    public void onResult() {
        adapter.initData(dataList);
        binding.swipeRefreshLayout.setRefreshing(false);
        binding.recyclerView.getAdapter().notifyDataSetChanged();
    }

    @Override
    public void onRefresh() {
        logic.execute();
    }
}
